package com.itboye.mengwu.app.login.ui;

import java.util.Observable;
import java.util.Observer;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;

public class RegiterActivity extends BaseActivity implements OnClickListener,
        Observer {

    EditText register_input_phonenum, register_yzm,
            register_input_password;
    Button register_now;
    Button getYzm;
    private String userName;
    private String pwd;
    MainPresenter mainPresenter;
    TimeCount count;
    GetYanzhengMaPresenter getYanzhengMaPresenter;
    private String phoneNumber;
    private String code;

    @Override
    public int initView() {
        // TODO Auto-generated method stub
        return R.layout.activity_regist;
    }

    @Override
    public void onMyCreate() {
        count = new TimeCount(60000, 1000, getYzm);
        mainPresenter = new MainPresenter(this);
        IntentFilter filter = new IntentFilter("finishregister");
        registerReceiver(receiver, filter);
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            finish();
        }
    };

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.register_now:
                userName = register_input_phonenum.getText().toString().trim();
                pwd = register_input_password.getText().toString().trim();
                code = register_yzm.getText().toString().trim();
                if (isEmpty(userName)) {
                    Toast.makeText(this, "用户名为空", 0).show();
                    return;
                }
                if (isEmpty(code)) {
                    Toast.makeText(this, "验证码为空", 0).show();
                    return;
                }
                if (isEmpty(pwd)) {
                    Toast.makeText(this, "密码为空", 0).show();
                    return;
                }

                    showProgressDialog("注册中,请稍后", true);
                    register_now.setEnabled(false);
                    mainPresenter.register(userName, pwd, code);
                break;
            case R.id.getYzm:
                phoneNumber = register_input_phonenum.getText().toString();
                if (!isEmpty(phoneNumber)) {
                    getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
                    getYanzhengMaPresenter.getVerificationCode(phoneNumber, 1 + "");
                    count.start();

                } else {
                    Toast.makeText(this, "电话号码为空", 0).show();
                }
                break;
            default:
                break;
        }
    }

    public boolean isEmpty(String msg) {
        if (msg == null || msg.equals(""))
            return true;
        return false;
    }

    @Override
    public void update(Observable arg0, Object arg1) {
        // TODO Auto-generated method stub
        ResultEntity entity = super.handlerError(arg1);
        if (entity != null) {
            if (entity.getEventType().equals(
                    GetYanzhengMaPresenter.YanzhengmaSuccessEvent)) {
//				code = entity.getData().toString();
//				register_test_password.setText("");
                register_now.setEnabled(true);
                ByAlert.alert(entity.getData() + "");
            } else if (entity.getEventType().equals(
                    GetYanzhengMaPresenter.YanzhengmaFailEvent)) {
                ByAlert.alert("验证码获取失败");
            } else if (entity.getEventType().equals(
                    MainPresenter.RegisterSuccessEvent)) {
                register_now.setEnabled(true);
                register_now.setText("注册");
                ByAlert.alert("注册成功");
                finish();
            } else if (entity.getEventType() == MainPresenter.RegisterFailureEvent) {
                Toast.makeText(this, entity.getData().toString(), 0).show();
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private void showDialog() {
        // TODO Auto-generated method stub
        final AlertDialog alertDialog = new AlertDialog.Builder(
                MainApplication.context).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.dialog_save);
        TextView tv_title = (TextView) window.findViewById(R.id.title);
        tv_title.setText("提示");
        TextView tv_message = (TextView) window.findViewById(R.id.message);
        tv_message.setText("是否保存编辑内容");
        Button btn_save = (Button) window.findViewById(R.id.save);
        Button btn_noSave = (Button) window.findViewById(R.id.noSave);
        Button canButton = (Button) window.findViewById(R.id.cancel);
        btn_save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainApplication.context,
                        LoginActivity.class);
                startActivity(intent);
            }
        });
        btn_noSave.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        canButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alertDialog.dismiss();
            }
        });

    }
}
